home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c++
- Subject: Re: Quick, easy question on externs...
- Date: Thu, 11 Jan 1996 02:19:23 GMT
- Organization: Netcom
- Message-ID: <30f46f5b.430017088@nntp.ix.netcom.com>
- References: <30F4333C.41A9@omni.voicenet.com>
- NNTP-Posting-Host: ix-dc9-08.ix.netcom.com
- X-NETCOM-Date: Wed Jan 10 6:18:00 PM PST 1996
- X-Newsreader: Forte Agent .99c/16.141
-
- David Zuckman <dzuckman@omni.voicenet.com> wrote:
-
- |>I got two source files:
- |>
- |>z1.cpp
- |>======
- |> #include <iostream.h>
- |>
- |> extern int const x;
- |>
- |> void main( void ) {
- |> x;
- |> }
- |>
- |>z2.cpp
- |>======
- |> int const x = 5;
- |>
- |>They compile and link fine (I'm using MS Visual C++ 2.1).
- |>
- |>I change the line in z1.cpp that reads
- |> x;
- |>to
- |> cout << x;
- |>and I get
- |>
- |> Incrementally linking...
- |> LINK : performing full link
- |> z1.obj : error LNK2001: unresolved external symbol "?x@@3HB
- (int const x)"
- |> WinDebug/dbx.exe : error LNK1120: 1 unresolved externals
- |>
- |>What gives?
-
- Looks like the optimizer allowed you to get away with incorrect code
- initially. What probably happend is that the compiler realized that
- x; doesn't do anything useful and eliminated the statement so there
- was no reference to x in the object module for z1.cpp.
-
- In C++, const implies static linkage so the x defined in z2.cpp is not
- available outside that file. To fix this, change the definition in
- z2.cpp to
-
- extern int const x = 5;
-
-
- Michael M Rubenstein
-